package com.bitaka.ws;

import com.bitaka.entity.NotifyBookingRS;
import dao.BilletDAO;
import entity.Billet;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author HABIBI AYOUB
 */
@WebService(serviceName = "CyberesaWS")
public class CyberesaWS {

    @EJB
    BilletDAO billetDAO = new BilletDAO();

    /**
     * Web service operation
     */
    @WebMethod(operationName = "NotifyBooking")
    public NotifyBookingRS NotifyBooking(@WebParam(name = "login") String login, @WebParam(name = "password") String password, @WebParam(name = "idAffiliate") int idAffiliate, @WebParam(name = "idOrder") int idOrder, @WebParam(name = "idProduct") int idProduct, @WebParam(name = "quantity") int quantity) throws IOException {

        System.out.println("Login = " + login);


        if (login.equals("adm") && password.equals("bitaka")) {
            String barcodes = "";
            NotifyBookingRS notifyBookingRS = new NotifyBookingRS();

            //remplire bares codes
            System.out.println("Generation billets");

            File fich = new File("c:\\OnLigne\\" + idOrder + ".txt");
            FileWriter fw = new FileWriter(fich);
            Date d1 = new Date();
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd/MM/yy HH:mm");
            String date1 = dateFormat1.format(d1);
            System.out.println("date = " + date1);
            for (int i = 0; i < quantity; i++) {
                Date d = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("ddHHmmssSSS");
                String date = dateFormat.format(d);
                String code = idOrder + idProduct + date + "-OL84";
                code=code.substring(1);
                fw.write(idProduct + ";" + idOrder + ";" + code + ";" + date1);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                }
                System.out.println(code);
                barcodes += code + ";";
                fw.write("\r\n");

            }
            fw.flush();
            fw.close();
            

//            List<Billet> billets = billetDAO.ticketEnLigne(idProduct, quantity);
//            for (Billet billet : billets) {
//                barcodes += billet.getNumero() + ";";
//
//            }
            
            System.out.println("Barcodes=" + barcodes);
            notifyBookingRS.setCode(0);
            notifyBookingRS.setMessage("Is done");
            notifyBookingRS.setIdOrder(idOrder);
            notifyBookingRS.setIdAffiliate(idAffiliate);
            notifyBookingRS.setIdProduct(idProduct);
            notifyBookingRS.setQuantity(quantity);
            notifyBookingRS.setBarCode(barcodes);
            return notifyBookingRS;
        } else {
            NotifyBookingRS notifyBookingRS = new NotifyBookingRS();
            notifyBookingRS.setCode(1);
            notifyBookingRS.setMessage("Authentification failed");
            notifyBookingRS.setIdOrder(idOrder);
            notifyBookingRS.setIdAffiliate(idAffiliate);
            notifyBookingRS.setIdProduct(idProduct);
            notifyBookingRS.setQuantity(quantity);
            notifyBookingRS.setBarCode("");
            return notifyBookingRS;
        }

    }
//    @WebMethod(operationName = "NotifyBooking")
//    public NotifyBookingRS NotifyBooking(@WebParam(name = "notifyBookingRQ") NotifyBookingRQ notifyBookingRQ) {
//        System.out.println("bookingRA == " + notifyBookingRQ.getIdAffiliate());
//         NotifyBookingRS booking = new NotifyBookingRS();
//        if ("adm".equals(notifyBookingRQ.getLogin())
//                && "bitaka".equals(notifyBookingRQ.getPassword())) {
//            // recupere les codes à bare des 
//            //tickets selon le nombre et le festival souhaiter
//            String code_barre = "2542544128GF-142";
//            List<String> barcodes = new ArrayList();
//            // chargement de List
//            
//            for (int i = 0; i < notifyBookingRQ.getQuantity(); i++) {
//                code_barre += i + "";
//                System.out.println(code_barre);
//                barcodes.add(code_barre);
//            }
//            
//            
//            
//            //asdasd
//            BarCodes bcodes = new BarCodes();
//            
//            bcodes.setBarCode(barcodes);
//            Status stat=new Status();
//            stat.setCode(0);
//            stat.setMessage("Is Done");
//            booking.setStatus(stat);
//            booking.setIdAffiliate(notifyBookingRQ.getIdAffiliate());
//            booking.setIdOrder(notifyBookingRQ.getIdOrder());
//            booking.setIdProduct(notifyBookingRQ.getIdProduct());
//            booking.setQuantity(notifyBookingRQ.getQuantity());
//            booking.setBarCodes(bcodes);
//        } else {
//            booking.getStatus().setCode(1);
//            booking.getStatus().setMessage("Athentiication Failed");
//            booking.setIdOrder(notifyBookingRQ.getIdOrder());
//            booking.setIdProduct(notifyBookingRQ.getIdProduct());
//            booking.setQuantity(notifyBookingRQ.getQuantity());
//
//        }
//        return booking;
//    }
}
